﻿namespace WMS6.Web.TaskMgmt
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TaskMgtServiceRef;

    public class UserLicense : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected HtmlForm F6041;
        protected GridView gvUserLienseDetail;
        protected HtmlInputHidden hdnCopy;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLine;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnUserLicenseID;
        protected HtmlHead Head1;
        protected HtmlImage img1;
        protected HtmlImage img2;
        protected HtmlImage img3;
        protected Label lblEquipmentProfileID;
        protected Label lblMsg;
        protected Label lblTaskStrategyID;
        protected Label lblUserGroup;
        protected Label lblUserID;
        protected Label lblUserLicense;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected TextBox txtEquipmentProfileID;
        protected TextBox txtTaskStrategyID;
        protected TextBox txtUserGroup;
        protected TextBox txtUserID;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtUserID.Text.Trim()))
            {
                this.lblMsg.Text = "请输入用户！";
            }
            else if (string.IsNullOrEmpty(this.txtUserGroup.Text.Trim()))
            {
                this.lblMsg.Text = "请输入用户组！";
            }
            else
            {
                if (string.IsNullOrEmpty(this.hdnUserLicenseID.Value))
                {
                    this.InsertUserLicense();
                }
                base.Response.Redirect(string.Format("UserLicenseDetail.aspx?from={0}&ID={1}&LineID={2}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtUserID.Text.Trim()), base.UrlEncode(this.hdnLineID.Value)), true);
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtUserID.Text = "";
            this.txtUserID.Enabled = true;
            base.BindData2GridView(this.gvUserLienseDetail, new List<TaskManagerUserDetailInfo>());
            base.DataEntityViewState = null;
            this.hdnFlag.Value = "";
            this.hdnUserLicenseID.Value = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtUserID.Text))
            {
                try
                {
                    List<string> UserID = new List<string> {
                        this.txtUserID.Text.Trim()
                    };
                    this.lblMsg.Text = TaskMgtServiceClientProxy.GetInstance().DeleteTaskManagerUser(UserID) + "删除成功！";
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                    this.hdnFlag.Value = "";
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtUserID.Text.Trim()) && (this.hdnFlag.Value == "1"))
            {
                List<string> lineID = base.GetSelectedIDsInGridView(this.gvUserLienseDetail, "LineID");
                try
                {
                    this.lblMsg.Text = TaskMgtServiceClientProxy.GetInstance().DeleteTaskManagerUserDetail(this.txtUserID.Text, lineID) + base.GetGlobalMsgResource("C006");
                    this.SetTaskinfoForm();
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C012") + ex.Message;
                }
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2440_004");
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            base.Response.Redirect("UserLicenseList.aspx?from=edit", false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtUserID.Text.Trim()))
            {
                this.lblMsg.Text = "请输入用户！";
            }
            else if (string.IsNullOrEmpty(this.txtUserGroup.Text.Trim()))
            {
                this.lblMsg.Text = "请输入用户组！";
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateUserLicense();
            }
            else
            {
                this.InsertUserLicense();
            }
        }

        protected void gvUserLienseDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F6041", this.gvUserLienseDetail);
        }

        protected void gvUserLienseDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as TaskManagerUserInfo).TaskManagerUserDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvUserLienseDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvUserLienseDetail.PageIndex * this.gvUserLienseDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvUserLienseDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                TaskManagerUserInfo taskManagerUserInfo = base.DataEntityViewState as TaskManagerUserInfo;
                DataView dv = CADataConverter.ToDataTable(taskManagerUserInfo.TaskManagerUserDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvUserLienseDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertUserLicense()
        {
            try
            {
                TaskManagerUserInfo taskManagerUserInfo = new TaskManagerUserInfo {
                    UserID = this.txtUserID.Text,
                    UserGroup = this.txtUserGroup.Text,
                    EquipmentProfileID = this.txtEquipmentProfileID.Text,
                    TaskStrategyID = this.txtTaskStrategyID.Text,
                    CreatedBy = base.UserInfo.UserID,
                    PriorityTaskType = "",
                    StrategyID = "",
                    LastCaseIdPicked = "",
                    LastWaveID = "",
                    LastLoc = ""
                };
                this.lblMsg.Text = TaskMgtServiceClientProxy.GetInstance().InsertTaskManagerUser(taskManagerUserInfo) + "保存成功！";
                base.DataEntityViewState = taskManagerUserInfo;
                this.txtUserID.Enabled = false;
                this.hdnFlag.Value = "1";
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "保存失败：" + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnUserLicenseID.Value = base.Request.QueryString["ID"].ToString();
                this.hdnFlag.Value = "1";
                this.SetTaskinfoForm();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
            if (string.IsNullOrEmpty(this.hdnLineID.Value))
            {
                this.hdnLineID.Value = "10";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        private void SetTaskinfoForm()
        {
            try
            {
                string UserLicenseID = this.hdnUserLicenseID.Value;
                TaskManagerUserInfo taskManagerUserInfo = TaskMgtServiceClientProxy.GetInstance().GetTaskManagerUserByID(this.hdnUserLicenseID.Value);
                base.DataEntityViewState = taskManagerUserInfo;
                if (taskManagerUserInfo != null)
                {
                    this.txtUserID.Text = taskManagerUserInfo.UserID;
                    this.txtUserID.Enabled = false;
                    this.txtUserGroup.Text = taskManagerUserInfo.UserGroup;
                    this.txtEquipmentProfileID.Text = taskManagerUserInfo.EquipmentProfileID;
                    this.txtTaskStrategyID.Text = taskManagerUserInfo.TaskStrategyID;
                    base.BindData2GridView(this.gvUserLienseDetail, taskManagerUserInfo.TaskManagerUserDetailInfos);
                    if ((taskManagerUserInfo.TaskManagerUserDetailInfos != null) && (taskManagerUserInfo.TaskManagerUserDetailInfos.Count > 0))
                    {
                        this.hdnLineID.Value = (Convert.ToInt32(taskManagerUserInfo.TaskManagerUserDetailInfos[taskManagerUserInfo.TaskManagerUserDetailInfos.Count - 1].LineID) + 10).ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateUserLicense()
        {
            try
            {
                TaskManagerUserInfo taskManagerUserInfo = base.DataEntityViewState as TaskManagerUserInfo;
                taskManagerUserInfo.UserGroup = this.txtUserGroup.Text;
                taskManagerUserInfo.EquipmentProfileID = this.txtEquipmentProfileID.Text;
                taskManagerUserInfo.TaskStrategyID = this.txtTaskStrategyID.Text;
                taskManagerUserInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = TaskMgtServiceClientProxy.GetInstance().UpdateTaskManagerUser(taskManagerUserInfo) + "更新成功！";
                base.DataEntityViewState = taskManagerUserInfo;
                this.txtUserID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "保存失败：" + ex.Message;
            }
        }
    }
}

